var database = require('../../data/data.js');
Page({
  data: {
    search_data: "",
    result: 1
  },
  onLoad: function (options) {
    var keys = wx.getStorageInfoSync().keys;
    if (keys != "") {
      var historys = [];
      for (var i = 0; i < keys.length; i++) {
        var history = {
          value: "",
          length: ""
        }
        var value = wx.getStorageSync(keys[i])
        history.value = value
        history.length = value.length
        historys[i] = history
      }
      this.setData({
        historys: historys
      })
    }
  },
  save_data: function (e) {
    this.setData({
      search_data: e.detail.value
    })
  },
  search: function () {
    var that = this;
    var flag = wx.getStorageSync("history" + 1);
    if (flag == "") {
      wx.setStorageSync("history" + 1, this.data.search_data)
    } else {
      var flag1 = true;
      var historys_keys = wx.getStorageInfoSync().keys
      for (var i = 0; i < historys_keys.length; i++) {
        if (wx.getStorageSync(historys_keys[i]) == this.data.search_data) {
          flag1 = false;
        }
      }
      if (flag1) {
        if (historys_keys.length == 10){
          for (var i = historys_keys.length; i > 1; i--) {
            wx.setStorageSync("history" + i, wx.getStorageSync(historys_keys[i - 2]))
          }
        } else {
          for (var i = historys_keys.length; i > 0; i--) {
            wx.setStorageSync("history" + (i + 1), wx.getStorageSync(historys_keys[i - 1]))
          }
        }
        wx.setStorageSync("history1", this.data.search_data)
      }
    }
    wx.request({
      url: database.url + '/project/getContestBySearch.action',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      data: {
        require: that.data.search_data
      },
      success(res) {
        database.count++;
        if (database.count > database.all - 2) {
          wx.hideLoading();
        }
        that.setData({
          result: res.data.result
        })
        if (res.data.result == 0) {
          var date1 = new Date();
          var contents = res.data.contests;
          var teams = [];
          var index = 0;
          var contentss = [];
          for (var i = 0; i < contents.length; i++) {
            var reg = new RegExp("-", "g");
            var newTime = contents[i].postdate.replace(reg, "/");
            var date = new Date(newTime);
            var seconds = 0;
            var minutes = 0;
            var hour = 0;
            var day = 0;
            var month = 0;
            var year = 0;
            var timeDifference = "";
            if ((date1 - date) / 1000 >= 60) {
              if ((date1 - date) / 1000 / 60 >= 60) {
                if ((date1 - date) / 1000 / 60 / 60 >= 24) {
                  if ((date1 - date) / 1000 / 60 / 60 / 24 >= 30) {
                    if ((date1 - date) / 1000 / 60 / 60 / 24 / 30 >= 12) {
                      year = parseInt((date1 - date) / 1000 / 60 / 60 / 24 / 30 / 12);
                      timeDifference = year + "年前";
                    } else {
                      month = parseInt((date1 - date) / 1000 / 60 / 60 / 24 / 30);
                      timeDifference = month + "个月前";
                    }
                  } else {
                    day = parseInt((date1 - date) / 1000 / 60 / 60 / 24);
                    timeDifference = day + "天前";
                  }
                } else {
                  hour = parseInt((date1 - date) / 1000 / 60 / 60);
                  timeDifference = hour + "小时前";
                }
              } else {
                minutes = parseInt((date1 - date) / 1000 / 60);
                timeDifference = minutes + "分钟前·";
              }
            } else {
              seconds = parseInt((date1 - date) / 1000);
              timeDifference = seconds + "秒前";
            }
            var content = {
              address: contents[i].adress,
              cost: contents[i].cost,
              date: contents[i].date,
              distance: contents[i].distance,
              gameid: contents[i].gameid,
              person: contents[i].person,
              pteamid: contents[i].pteamid,
              popenid: contents[i].popenid,
              sort: contents[i].sort,
              time: contents[i].time,
              week: contents[i].week,
              teamLogo: contents[i].pteamlogo,
              title: contents[i].pteamname,
              postdate: contents[i].postdate,
              timeDifference: timeDifference
            }
            contentss[i] = content;
            that.setData({
              contents: contentss
            })
          }
        }
      }
    })
  },
  history: function (e) {

    var that = this;
    wx.request({
      url: database.url + '/project/getContestBySearch.action',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      data: {
        require: e.currentTarget.dataset.history
      },
      success(res) {
        database.count++;
        if (database.count > database.all - 2) {
          wx.hideLoading();
        }
        that.setData({
          result: res.data.result
        })
        if (res.data.result == 0) {
          var date1 = new Date();
          var contents = res.data.contests;
          var teams = [];
          var index = 0;
          var contentss = [];
          for (var i = 0; i < contents.length; i++) {
            var reg = new RegExp("-", "g");
            var newTime = contents[i].postdate.replace(reg, "/");
            var date = new Date(newTime);
            var seconds = 0;
            var minutes = 0;
            var hour = 0;
            var day = 0;
            var month = 0;
            var year = 0;
            var timeDifference = "";
            if ((date1 - date) / 1000 >= 60) {
              if ((date1 - date) / 1000 / 60 >= 60) {
                if ((date1 - date) / 1000 / 60 / 60 >= 24) {
                  if ((date1 - date) / 1000 / 60 / 60 / 24 >= 30) {
                    if ((date1 - date) / 1000 / 60 / 60 / 24 / 30 >= 12) {
                      year = parseInt((date1 - date) / 1000 / 60 / 60 / 24 / 30 / 12);
                      timeDifference = year + "年前";
                    } else {
                      month = parseInt((date1 - date) / 1000 / 60 / 60 / 24 / 30);
                      timeDifference = month + "个月前";
                    }
                  } else {
                    day = parseInt((date1 - date) / 1000 / 60 / 60 / 24);
                    timeDifference = day + "天前";
                  }
                } else {
                  hour = parseInt((date1 - date) / 1000 / 60 / 60);
                  timeDifference = hour + "小时前";
                }
              } else {
                minutes = parseInt((date1 - date) / 1000 / 60);
                timeDifference = minutes + "分钟前·";
              }
            } else {
              seconds = parseInt((date1 - date) / 1000);
              timeDifference = seconds + "秒前";
            }
            var content = {
              address: contents[i].adress,
              cost: contents[i].cost,
              date: contents[i].date,
              distance: contents[i].distance,
              gameid: contents[i].gameid,
              person: contents[i].person,
              pteamid: contents[i].pteamid,
              popenid: contents[i].popenid,
              sort: contents[i].sort,
              time: contents[i].time,
              week: contents[i].week,
              teamLogo: contents[i].pteamlogo,
              title: contents[i].pteamname,
              postdate: contents[i].postdate,
              timeDifference: timeDifference
            }
            contentss[i] = content;
            that.setData({
              contents: contentss
            })
          }
        }
      }
    })
  },
  detail: function (e) {
    wx.navigateTo({
      url: '/pages/fight_detail/battle/battle?gameid=' + e.currentTarget.dataset.gameid + '&popenid=' + e.currentTarget.dataset.popenid,
    })
  },
})